'use strict'

import React, {Component} from 'react';
import {
  Navigator,
  TouchableHighlight,
  TouchableOpacity,
  Text,
  View,
} from 'react-native';

import Platform from 'Platform';
import Config from './config';
import Constants from './common/Constants';
import {ObjUtils} from './utils/Utils';

export default class TNavigator extends Component {
  constructor(props) {
    super(props);
    this._init = true;
  }

  componentDidMount() {

  }

  compoentWillUnMount() {
    this.props.routes.dispose();
  }

  render() {
    return (
      <Navigator
        ref="navigator"
        style={{}}
        configureScene={this._configureScene.bind(this)}
        initialRoute={this._getInitRoute()}
        renderScene={this._renderScene.bind(this)}
      />
    );
  }

  _configureScene(route, routeStack) {
    // console.log("configureScene", route);
    let configure = Navigator.SceneConfigs.FloatFromRight;
    if(route.isBack) {
      return Navigator.SceneConfigs.FloatFromLeft;
    }
    return {
      ...configure,
      gestures: {
        jumpBack: this._overrideNavigatorGesturesJumpBack.bind(this),
      }
    }
  }

  _getInitRoute() {
    var authorization = this.props.dataManager.callMethod(Constants.MODULE_TYPE.LOGIN, "getAutorization");
    var version = this.props.dataManager.callMethod(Constants.MODULE_TYPE.COMMON, "getSaveVersion");
    var selfData = this.props.dataManager.callMethod(Constants.MODULE_TYPE.USER, "getSelfData");
    var pageData = null
    if(version && version.version_code != Config.version_code && Config.show_welcome_in_new_version === true) {
      pageData = Config.pages.welcome;
      this.props.dataManager.callMethod(Constants.MODULE_TYPE.COMMON, "saveVersion", [Config.version_code]);
      // this.props.dataManager.callMethod(Constants.MODULE_TYPE.LOGIN, "clearLoginData");
      // authorization = "0";
    }
    else if(authorization != null && authorization != "0") {
      // return {pageData:Config.pages.activity};
      // pageData = Config.pages.login_provision;
      // pageData = Config.pages.quanzi_welcome;
      // pageData = Config.pages.quanzi_post_content;
      // pageData = Config.pages.vipactivitypage;
      //  pageData = Config.pages.heart_eap_main;
      //  pageData = Config.pages.sport_dibiao_main;
       pageData = Config.pages.loginAdvertPage;
      // pageData = Config.pages.user_notice;
      //  pageData = Config.pages.jifenmall_qiandao_page;
      // pageData = Config.pages.qz_qunliao_create;
      // return {pageData:Config.pages.activity_entrySinger};
    }
    else {
      // pageData = Config.pages.bind;
      // pageData = Config.pages.login_provision;
      pageData = Config.pages.login;
    }
    pageData.routeData = {authorization:authorization, userData:selfData};
    pageData = ObjUtils.copy(pageData);
    pageData.routeID = 0;
    return {
      pageData:pageData,
      gestrues: {
        jumpBack: this._overrideNavigatorGesturesJumpBack.bind(this),
      }
    }
  }

  _overrideNavigatorGesturesJumpBack() {
    // console.log("deal navigator gestures jump back");
  }

  _renderScene(route, navigator) {
    // console.log(route, navigator);
    if(this._init) {
      this._init = false;
      this.props.routes.setup(navigator, route.pageData, this.props.dataManager);
      this.props.routes.setCurrentPage(route.pageData);
    }
    var page = this._getComponent(route.pageData, navigator, route.params);
    return page;
  }

  _getComponent(pageData, navigator, params) {
    var page = this.props.routes.getComponent(pageData.key);
    return <page.component navigator={navigator} pageData={pageData} {...params}/>;;
  }
}
